﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Database;
using System.Data;
using ShoppingCart;

public partial class computer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            showComputerType();
            
            if (Request["man"] == null)
            {
                showComputer();
            }
            else {
                int man = Convert.ToInt32(Request["man"]);
                showComputerByMan(man);
            }
        }
        

    }
    void showComputerByMan(int man)
    {
        MayTinh mt = new MayTinh();
        DataList1.DataSource = mt.DanhSachSPTheoHangSX(man);
        DataList1.DataBind();
    }
    void showComputerType()
    {
        LoaiMayTinh computerType = new LoaiMayTinh();
        DataSet ds = computerType.DSLoaiMayTinh();
        foreach (DataRow masterRow in ds.Tables[0].Rows)
        {
            MenuItem masterItem = new MenuItem((string)masterRow["TenLoai"]);
            loaimt.Items.Add(masterItem);

        }
            
    }
    void showComputer()
    {
        MayTinh lk = new MayTinh();
        //DataList1.DataSource = lk.DSMayTinh();
        //DataList1.DataBind();
        PagedDataSource objPds = new PagedDataSource();
        DataSet ds = lk.DSMayTinh();
        objPds.DataSource = ds.Tables[0].DefaultView;// ds.DefaultViewManager;
        objPds.AllowPaging = true;
        objPds.PageSize = 6;
        objPds.CurrentPageIndex = this.currentPage;
        lblCurrentPage.Text = "Page: " + (this.currentPage + 1).ToString() + " of " + objPds.PageCount.ToString();
        cmdPrev.Enabled = !objPds.IsFirstPage;
        cmdNext.Enabled = !objPds.IsLastPage;
        DataList1.DataSource = objPds;
        //DataList1.DataSource = lk.DSLinhKien();
        DataList1.DataBind();
    }
    public int currentPage
    {
        get
        {
            object o = this.ViewState["_CurrentPage"];
            if (o == null)
                return 0; // default to showing the first page
            else
                return (int)o;
        }
        set
        {
            this.ViewState["_CurrentPage"] = value;
        }
    }
    protected void cmdPrev_Click(object sender, System.EventArgs e)
    {
        currentPage -= 1;
        showComputer();
    }

    protected void cmdNext_Click(object sender, System.EventArgs e)
    {
        currentPage += 1;
        showComputer();

    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Mua")
        {
            int ma = Convert.ToInt32(e.CommandArgument.ToString());
            MayTinh mt = new MayTinh();
            DataTable product = mt.MayTinhTheoMa(ma);
            Item item = new Item();
            DataRow row = product.Rows[0];
           
            item.ID = Convert.ToInt32(row["mamay"].ToString());
            item.Name = row["tenmay"].ToString();
            item.Image = row["hinhanh"].ToString();
            item.Cost = Convert.ToDouble(row["giaban"].ToString());
            item.Quantity = 1;
            item.Total = Convert.ToDouble(row["giaban"].ToString());

            
            if (Session["ShoppingCart"] == null)
            {
                Session["ShoppingCart"] = new ShoppingCart.Cart();
            }

            ShoppingCart.Cart cart = (ShoppingCart.Cart)Session["ShoppingCart"];
            cart.AddToCart(item);
            Session["ShoppingCart"] = cart;
            /*DataTable Gio_hang = (DataTable)Session["Gio_hang"];
            //nếu đã có tăng lên 1
            bool co = false;
            for (int i = 0; i < Gio_hang.Rows.Count; i++)
                if (int.Parse(Gio_hang.Rows[i]["ma"].ToString()) == int.Parse(e.CommandArgument.ToString()))
                {
                    Gio_hang.Rows[i]["soluong"] = int.Parse(Gio_hang.Rows[i]["soluong"].ToString()) + 1;
                    co = true;
                    break;
                }
            //nếu chưa có
            if (co == false)
            {
                //DataTable dt  = (DataTable)
                DataRow d = Gio_hang.NewRow();
                d["ma"] = e.CommandArgument;
                Database.GioHang tblSanPham = new Database.GioHang("linhkien", "select * from linhkien where malinhkien = " + e.CommandArgument);
                //doc bang
                d["ten"] = tblSanPham.Rows[0]["tenlinhkien"];
                d["giaban"] = tblSanPham.Rows[0]["giaban"];
                d["soluong"] = 1;
                d["hinhanh"] = tblSanPham.Rows[0]["hinhanh"];

                Gio_hang.Rows.Add(d);

            }
            Session["Gio_hang"] = Gio_hang;*/
            string links = Request.ServerVariables["HTTP_REFERER"].ToString();
            Response.Redirect(links);
        }
    
        else { 
                string urlDetail = "~/computerdetail.aspx?man=" + e.CommandArgument.ToString();
                Response.Redirect(urlDetail);
            }
        
    }
    protected void Imagegiohang_Click(object sender, ImageClickEventArgs e)
    {
    }

    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void loaimt_MenuItemClick(object sender, MenuEventArgs e)
    {
        Label4.Text = "You selected " + e.Item.Text + ".";
        showComputerByType(e.Item.Text);
    }
    void showComputerByType(string loai) {
        MayTinh mt = new MayTinh();
        DataList1.DataSource = mt.DanhSachMTTheoTenLoaiLK(loai);
        DataList1.DataBind();
    }
}